package com.hm.recycle.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.querydsl.QPageRequest;
import org.springframework.stereotype.Service;

import com.hm.common.model.PageInfo;
import com.hm.common.util.DbDocumentDef;
import com.hm.recycle.mongo.domain.DbDocumentDeleteRecycle;
import com.hm.recycle.mongo.domain.QDbDocumentDeleteRecycle;
import com.hm.recycle.mongo.vo.DeleteRecycleVo;
import com.hm.recycle.share.repository.DbDocumentDeleteRecycleRepository;
import com.hm.recycle.share.service.DbDocumentDeleteRecycleService;

/**
 * @author shishun.wang
 * @date 2016年5月23日 下午11:08:24
 * @version 1.0
 * @describe 
 */
@Service
@SuppressWarnings({ "rawtypes", "unchecked" })
public class DbDocumentDeleteRecycleServiceImpl implements DbDocumentDeleteRecycleService{

	@Autowired
	private DbDocumentDeleteRecycleRepository dbDocumentRecycleRepository;
	
	@Override
	public String addDbDocumentRecycle(DbDocumentDeleteRecycle dbDocumentRecycle) {
		return dbDocumentRecycleRepository.save(dbDocumentRecycle).getId();
	}

	@Override
	public PageInfo<DeleteRecycleVo> queryDelRecycle(DbDocumentDef documentDef,
			String operationUser, Integer version, Long startTime,
			Long endTime, Integer pageNumber, Integer pageSize) {
		PageInfo<DeleteRecycleVo> pageInfo = new PageInfo<DeleteRecycleVo>(pageNumber, pageSize);
		
		QDbDocumentDeleteRecycle dbDocumentDelRecycle = QDbDocumentDeleteRecycle.dbDocumentDeleteRecycle;
		Pageable pageable = new QPageRequest(pageInfo.getPageNumber(), pageInfo.getPageSize(),
				dbDocumentDelRecycle.createTime.desc());
		
		List<DeleteRecycleVo> delRecycleVos = new ArrayList<DeleteRecycleVo>();

		Page page = dbDocumentRecycleRepository.findAll(pageable);
		List<DbDocumentDeleteRecycle> content = page.getContent();
		for (DbDocumentDeleteRecycle delRecycle : content) {
			DeleteRecycleVo delRecycleVo = new DeleteRecycleVo();
			{
				delRecycleVo.setId(delRecycle.getId());
				delRecycleVo.setDocumentDef(delRecycle.getDocumentDef());
				delRecycleVo.setTargetContent(delRecycle.getTargetContent());
			}
			
			delRecycleVos.add(delRecycleVo);
		}
		return pageInfo.execute(page, delRecycleVos);
	}

	
}
